package com.rp.system.common.controller;


import cn.hutool.core.io.file.FileReader;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rp.common.aspect.annotetion.ResponseResultBody;
import com.rp.common.result.ResultDTO;
import com.rp.common.aspect.annotetion.AutoLog;
import com.rp.common.utils.NullIs;
import com.rp.system.common.entity.Location;
import com.rp.system.common.service.LocationService;
import com.rp.system.common.service.impl.LocationServiceImpl;
import com.rp.common.utils.Selectway;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author vicente
 * @since 2020-12-10
 */
@RestController
@RequestMapping("/location")
@Api(tags = "位置管理")
@ResponseResultBody
public class LocationController {

    @Autowired
    private LocationService locationService;

    @Autowired
    private Selectway selectway;

    @AutoLog(value = "位置管理-分页列表")
    @GetMapping("/list")
    @ApiOperation(value = "分页列表")
    @RequiresPermissions("location:list")
    public ResultDTO<IPage<Location>> list(
            @RequestParam(defaultValue = "1",required = false) @ApiParam(value = "当前页面") int pageNum,
            @RequestParam(defaultValue = "20",required = false) @ApiParam(value = "每页显示的数量") int pageSize,
            @RequestParam @ApiParam(value = "查询级别",required = true) Integer rank,
            @RequestParam(required = false) @ApiParam(value = "模糊查询字符串") String string){
        QueryWrapper<Location> queryWrapper = new QueryWrapper<Location>();
        selectway.fields(queryWrapper,string,"location",null,null);
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("length(loc_code)", rank * (LocationServiceImpl.BUILD_MAX_SIZE + 1));
        return ResultDTO.success().data(locationService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }

    @AutoLog(value = "位置管理-增加")
    @PostMapping("/insert")
    @ApiOperation(value = "增加")
    @RequiresPermissions("location:update")
    public ResultDTO insert(@RequestBody Location location){
        return ResultDTO.tf(locationService.save(location));
    }

     /*
    *
    * {
     "zone_id": "416",
     "zone_name": "蓬管所小区",
     "zone_address": "七里河区三角线144-166号",
     "product_id": "4",
     "lease_group_id": "68",
     *
     "zone_number": "2",
     "zone_households": "",
     "zone_area": "6425",
     "zone_equity": "",
     *
           "zone_remark": "",
     "zone_developers": "",
     "zone_estate": "",
     "zone_pmo": "",
     "zone_oc": "",
         "zone_longitude": "103.763553,36.07553",
     "committee_id": "",
     "street_id": "",
     "zone_vrurl": "",
     "lease_group_cooperative_corporation": "中国铁路兰州局集团有限公司兰州房建段"
 }
    * */

    @AutoLog(value = "位置管理-daoRu")
    @PostMapping("/daoRu")
    @ApiOperation(value = "daoRu")
    public ResultDTO daoRu(){
        FileReader fileReader = new FileReader("C:/Users/90504/Desktop/无标题.json");
        locationService.remove(new QueryWrapper<Location>().eq("length(loc_code)", 12).eq("org_code", "A04"));
        String result = fileReader.readString();
        JSONArray jSONArray = JSONArray.parseArray(result);
        for (Object object : jSONArray) {
            Map<String, Object> map = JSONObject.parseObject(String.valueOf(object), Map.class);
            Location location = new Location();
            location.setName((String) map.get("zone_name"));
            location.setAddress((String) map.get("zone_address"));
            String id = locationService.getOne(new QueryWrapper<Location>().select("id").eq("name", map.get("lease_group_cooperative_corporation")).eq("length(loc_code)", 6)).getId();
            if(NullIs.isEmpty(id)){
                return ResultDTO.bad();
            }
            location.setParentId(id);
            location.setMemo((String) map.get("zone_remark"));
            location.setLongitude((String) map.get("zone_longitude"));
            HashMap<String,Object> resMap = new HashMap();

            resMap.put("buildNumber", map.get("zone_number"));
            resMap.put("households", map.get("zone_households"));
            resMap.put("area", map.get("zone_area"));
            resMap.put("equity", map.get("zone_equity"));
            resMap.put("developers", map.get("zone_developers"));
            resMap.put("estate", map.get("zone_estate"));
            resMap.put("propertyManagementOfficeContactPerson", map.get("zone_pmo"));
            resMap.put("industryAuthorityContact", map.get("zone_oc"));
            resMap.put("VRUrl", map.get("zone_vrurl"));
            location.setExpand(JSON.toJSONString(resMap));
            boolean boo = locationService.save(location);
        }
        return ResultDTO.success();
    }



    @AutoLog(value = "位置管理-修改")
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    @RequiresPermissions("location:update")
    public ResultDTO update(@RequestBody Location location){
        return ResultDTO.tf(locationService.updateById(location));
    }

    @AutoLog(value = "位置管理-删除")
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
    @RequiresPermissions("location:update")
    public ResultDTO delete(@RequestBody @ApiParam(value = "id", required = true) String id){
        return ResultDTO.tf(locationService.removeById(id));
    }

    @AutoLog(value = "位置管理-批量删除")
    @DeleteMapping("/deleteAll")
    @ApiOperation(value = "批量删除")
    @RequiresPermissions("location:update")
    public ResultDTO deleteAll(@RequestBody @ApiParam(value = "id(英文逗号隔开)", required = true) String ids){
        return ResultDTO.tf(locationService.removeByIds(Arrays.asList(ids.split(","))));
    }

    @AutoLog(value = "位置管理-不分页列表")
    @GetMapping("/listAll")
    @ApiOperation(value = "不分页列表")
    @RequiresPermissions("location:list")
    public ResultDTO<List<Location>> listAll(@RequestParam @ApiParam(value = "查询级别",required = true) Integer rank){
        QueryWrapper<Location> queryWrapper = new QueryWrapper<Location>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("length(loc_code)", rank * (LocationServiceImpl.BUILD_MAX_SIZE + 1));
        return ResultDTO.success().data(locationService.list(queryWrapper));
    }

    @AutoLog(value = "位置管理-根据id查询子集")
    @GetMapping("/findSonById")
    @ApiOperation(value = "根据id查询子集")
    @RequiresPermissions("location:list")
    public ResultDTO<List<Location>> findSonById(@RequestParam @ApiParam(value = "主键ID",required = true) String id){
        QueryWrapper<Location> queryWrapper = new QueryWrapper<Location>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("parent_id", id);
        return ResultDTO.success().data(locationService.list(queryWrapper));
    }

    @AutoLog(value = "位置管理-根据code查询子集")
    @GetMapping("/findSonByLocCode")
    @ApiOperation(value = "根据code查询子集")
    @RequiresPermissions("location:list")
    public ResultDTO<List<Location>> findSonByLocCode(@RequestParam @ApiParam(value = "位置编码",required = true) String locCode){
        QueryWrapper<Location> queryWrapper = new QueryWrapper<Location>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.likeRight("loc_code", locCode);
        queryWrapper.eq("length(loc_code)", locCode.length() + LocationServiceImpl.BUILD_MAX_SIZE +1);
        return ResultDTO.success().data(locationService.list(queryWrapper));
    }
}
